<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="${pageContext.request.contextPath}/">
    <link href="bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    <%--bootstrap-table核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-table/bootstrap-table.min.css">
    <%--bootstrap-datetimepicker核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">

</head>
<body>
    <!--路径导航-->
    <ol class="breadcrumb">
        <li>工作台</li>
        <li class="active">市场活动</li>
    </ol>
    <div class="panel-body">
        <!--条件查询-->
        <div class="well well-sm">
            <form id="searchForm" class="form-inline" style="margin-bottom: 0">
                <div class="row" style="margin-bottom: 15px;">
                    <div class="form-group col-md-4">
                        <label>&emsp;&emsp;名称</label>
                        <input type="text" id="name" class="form-control">
                    </div>
                    <div class="form-group col-md-4">
                        <label>开始日期</label>
                        <div class="input-group date myDatetime">
                            <input type="text" class="form-control" id="startDate" readonly/>
                            <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                        </div>
                    </div>
                    <div class="form-group col-md-4">
                        <label>结束日期</label>
                        <div class="input-group date myDatetime">
                            <input type="text" class="form-control" id="endDate" readonly/>
                            <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-md-4">
                        <label>&emsp;所有者</label>
                        <select id="owner" class="form-control ownerSelect" style="width: 196px;">
                        </select>
                    </div>
                    <div class="form-group col-md-4">
                        <button type="button" class="btn btn-primary" onclick="$('#tab').bootstrapTable('refresh');">
                            <span class="glyphicon glyphicon-search"> 搜索</span>
                        </button>
                        <button type="button" class="btn btn-primary"
                                onclick="$('#searchForm')[0].reset();$('#tab').bootstrapTable('refresh');">
                            <span class="glyphicon glyphicon-refresh"> 清空</span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!--工具栏和表格-->
        <div id="toolbar">
            <button type="button" class="btn btn-primary" onclick="$('#saveWin').modal('show');">
                <span class="glyphicon glyphicon-plus"> 新增</span>
            </button>
            <button type="button" class="btn btn-danger" onclick="removeBatch()">
                <span class="glyphicon glyphicon-trash"> 批量删除</span>
            </button>
        </div>
        <table id="tab" class="table table-hover table-striped table-bordered"></table>
    </div>
    <!--模态窗-->
    <div id="saveWin" class="modal fade dataWin">
        <div class="modal-dialog" style="width: 90%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title">新增市场活动</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal dataForm">
                        <div class="form-group">
                            <div class="errorMsg" style="text-align: center;color: red;"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                            <div class="col-sm-10" style="width: 300px;">
                                <select id="save-owner" class="form-control ownerSelect">
                                </select>
                            </div>
                            <label class="col-sm-2 control-label">名称</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="save-name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">开始日期</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="save-startDate" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                            <label class="col-sm-2 control-label">结束日期</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="save-endDate" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">成本</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="save-cost">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">描述</label>
                            <div class="col-sm-10" style="width: 60%;">
                                <textarea class="form-control" rows="3" id="save-description"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="save()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <div id="editWin" class="modal fade dataWin">
        <div class="modal-dialog" style="width: 90%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title">编辑市场活动</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal dataForm">
                        <input type="hidden" id="edit-id">
                        <div class="form-group">
                            <div class="errorMsg" style="text-align: center;color: red;"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                            <div class="col-sm-10" style="width: 300px;">
                                <select id="edit-owner" class="form-control ownerSelect">
                                </select>
                            </div>
                            <label class="col-sm-2 control-label">名称</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="edit-name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">开始日期</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="edit-startDate" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                            <label class="col-sm-2 control-label">结束日期</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="edit-endDate" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">成本</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="edit-cost">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">描述</label>
                            <div class="col-sm-10" style="width: 60%;">
                                <textarea class="form-control" rows="3" id="edit-description"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="edit()">保存</button>
                </div>
            </div>
        </div>
    </div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<%--bootstrap-table核心js--%>
<script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
<%--bootstrap-table中文语言包--%>
<script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<%--bootstrap-datetimepicker核心js--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<%--bootstrap-datetimepicker中文语言包--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
</body>
<script type="text/javascript">
    $(function () {
        initTable();
        initRelation();
        //清空模态框中的表单
        $('#saveWin').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[0].reset();
        });

        $('#editWin').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[1].reset();
        });

        $('.myDatetime').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd', //日期格式
            weekStart: 1, //一周从周一开始
            autoclose: true, //选择日期后关闭
            startView: 'month', //默认打开月
            minView: 'month',   //精确到年月日，不带时分秒
            todayBtn: true,
            forceParse: false
        });
    });

    //初始化表格
    function initTable() {
        //调用组件配置异步表格
        $('#tab').bootstrapTable({
            //http请求方式
            method: 'get',
            url: 'activity/page',
            //工具栏
            toolbar: '#toolbar',
            //点击选中
            clickToSelect: true,
            //单选
            singleSelect: false,
            //处理响应数据格式
            responseHandler: function (res) {
                if (res.code == 0) {
                    return {
                        rows: res.data.list,
                        total: res.data.total
                    };
                }
            },
            //开启分页
            pagination: true,
            //设置服务器端分页
            sidePagination: 'server',
            //设置每页条数
            pageSize: 10,
            //设置每页条数列表
            pageList: [10, 20, 50, 100],
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            //设置bootstrapTable向后台提交分页数据
            queryParamsType: '',
            queryParams: function (params) {
                params.name = $('#name').val();
                params.owner = $('#owner').val();
                params.startDate = $('#startDate').val();
                params.endDate = $('#endDate').val();
                return params;
            },
            showRefresh: true,
            showToggle: true,
            showFullscreen: true,
            //表格中列描述
            columns: [
                {
                    checkbox: true
                }, {
                    field: 'id',
                    visible: false
                }, {
                    title: '名称',
                    field: 'name',
                    halign: 'center',
                    //列数据格式化
                    formatter: function (value, row) {
                        //格式化之后的列数据
                        return '<a href="activity/detail/' + row.id + '">' + value + '</a>';
                    }
                }, {
                    title: '所有者',
                    field: 'userName',
                    halign: 'center'
                }, {
                    title: '开始日期',
                    field: 'startDate',
                    halign: 'center'
                }, {
                    title: '结束日期',
                    field: 'endDate',
                    halign: 'center'
                }, {
                    title: '创建者',
                    field: 'createBy',
                    halign: 'center'
                }, {
                    title: '创建时间',
                    field: 'createTime',
                    halign: 'center'
                }, {
                    title: '修改者',
                    field: 'editBy',
                    halign: 'center'
                }, {
                    title: '修改时间',
                    field: 'editTime',
                    halign: 'center'
                }, {
                    title: '操作',
                    halign: 'center',
                    align: 'center',
                    formatter: function (value, row) {
                        return '<button type="button" title="编辑" class="btn btn-warning btn-xs" onclick="get(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-edit"></span></button>&emsp;' +
                            '<button type="button" title="删除" class="btn btn-danger btn-xs" onclick="remove(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-trash"></span></button>';
                    }
                }
            ]
        });
    }

    //加载关联信息，初始化下拉列表
    function initRelation() {
        $.ajax('user/list', {
            type: 'get',
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.name + '</option>';
                    });
                    $('.ownerSelect').html(str);
                }
            }
        })
    }

    //新增
    function save() {
        $.ajax('activity/add', {
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify({
                owner: $('#save-owner').val(),
                name: $('#save-name').val(),
                startDate: $('#save-startDate').val(),
                endDate: $('#save-endDate').val(),
                cost: $('#save-cost').val(),
                description: $('#save-description').val()
            }),
            beforeSend: function () {
                //前端数据校验
                var owner = $('#save-owner').val();
                if (!owner) {
                    $('.errorMsg').html('请选择所有者');
                    return false;
                }
                var name = $('#save-name').val();
                if (!name) {
                    $('.errorMsg').html('名称不能为空');
                    return false;
                }
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('新增成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#saveWin').modal('hide');
                }else{
                    $('.errorMsg').html(res.message);
                }
            }
        });
    }

    //打开修改
    function get(id) {
        $.ajax('activity/find/' + id, {
            type: 'get',
            success: function (res) {
                if (res.code == 0) {
                    $('#edit-id').val(res.data.id);
                    $('#edit-owner').val(res.data.owner);
                    $('#edit-name').val(res.data.name);
                    $('#edit-startDate').val(res.data.startDate);
                    $('#edit-endDate').val(res.data.endDate);
                    $('#edit-cost').val(res.data.cost);
                    $('#edit-description').val(res.data.description);
                    $('#editWin').modal('show');
                }else if(res.code = -100){
                    // location.href = 'page/login';
                    parent.location.href = 'page/login';
                }else{
                    alert(res.message);
                }
            }
        });
    }

    //修改
    function edit() {
        $.ajax('activity/edit', {
            type: 'put',
            contentType: 'application/json',
            data: JSON.stringify({
                id: $('#edit-id').val(),
                owner: $('#edit-owner').val(),
                name: $('#edit-name').val(),
                startDate: $('#edit-startDate').val(),
                endDate: $('#edit-endDate').val(),
                cost: $('#edit-cost').val(),
                description: $('#edit-description').val()
            }),
            beforeSend: function () {
                //前端数据校验
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('编辑成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#editWin').modal('hide');
                }else{
                    $('.errorMsg').html(res.message);
                }
            }
        });
    }

    //删除
    function remove(id) {
        if (confirm('确定删除数据吗？')) {
            $.ajax('activity/remove/' + id, {
                type: 'delete',
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            });
        }
    }

    //批量删除
    function removeBatch() {
        //获取用户在表格中选择
        var row = $('#tab').bootstrapTable('getSelections');
        if (row.length == 0) {
            alert('请选择需要删除的数据.');
        } else {
            if (!confirm('确定删除数据吗?')) {
                return;
            }
            var ids = [];
            $.each(row, function () {
                ids.push(this.id);
            });
            $.ajax('activity/remove', {
                type: 'post',
                data: {
                    _method: 'delete',
                    ids: ids.join()
                },
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            })
        }
    }
</script>
</html>